﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Xml;
using Util.Database;

namespace AutoNZBLoader
{
    public class Feed
    {
        private readonly string _Address;
        public string Address
        {
            get { return _Address; }
        }

        private readonly IList<FeedItem> _Items;
        public IList<FeedItem> Items
        {
            get { return _Items; }
        } 

        private Feed(string address)
        {
            this._Address = address;
            List<FeedItem> lst = new List<FeedItem>();

            try
            {
                using (System.IO.Stream ms = Util.Net.Download.DownloadData(address))
                {
                    XmlDocument d = new XmlDocument();
                    try
                    {
                        d.Load(ms);
                    }
                    catch (XmlException)
                    {
                        _Items = new System.Collections.ObjectModel.ReadOnlyCollection<FeedItem>(lst);
                        return;
                    }

                    XmlNodeList nodelist = d.GetElementsByTagName("item");
                    foreach (XmlNode n in nodelist)
                    {
                        string Title = "";
                        string Description = "";
                        string Link = "";
                        foreach (XmlNode c in n.ChildNodes)
                        {
                            if (c.Name.Equals("TITLE", StringComparison.OrdinalIgnoreCase))
                            {
                                Title = c.InnerText;
                            }
                            else if (c.Name.Equals("LINK", StringComparison.OrdinalIgnoreCase))
                            {
                                Link = c.InnerText;
                            }
                            else if (c.Name.Equals("DESCRIPTION", StringComparison.OrdinalIgnoreCase))
                            {
                                Description = c.InnerText;
                            }
                        }

                        try
                        {
                            lst.Add(new FeedItem(Title, Description, Link));
                        }
                        catch (ArgumentException) { }
                    }
                }
            }
            catch (Util.Net.DownloadException)
            {
            }

            _Items = new System.Collections.ObjectModel.ReadOnlyCollection<FeedItem>(lst);
        }


        private static IList<Feed> _Feeds;
        public static IList<Feed> Feeds
        {
            get
            {
                if (_Feeds == null)
                {
                    List<Feed> lst = new List<Feed>();

                    using (System.Data.IDbCommand cmd = DB.CreateCommand())
                    {
                        cmd.CommandText = "select * from feed";
                        using (IDataReader dr = cmd.ExecuteReader())
                        {
                            while (dr.Read())
                            {
                                string URL = dr["ADDRESS"].ToString();
                                lst.Add(new Feed(URL));
                            }
                        }
                    }
                    _Feeds = new System.Collections.ObjectModel.ReadOnlyCollection<Feed>(lst);
                }
                return _Feeds;
            }
        }
    }
}
